package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import orm.Adminstab;
import orm.AdminstabDAO;
import orm.HibernateSessionFactory;

public class LoginCheck implements Action,ServletResponseAware {
	private String account;
	private String psw;
	private HttpServletResponse response;
	//2312
	
	
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPsw() {
		return psw;
	}

	public void setPsw(String psw) {
		this.psw = psw;
	}

	public String execute(){
		ActionContext ctx = ActionContext.getContext();
		HibernateSessionFactory.getSession().beginTransaction();
		
		AdminstabDAO adminstabDAO = new AdminstabDAO();
		Adminstab adminstab = adminstabDAO.loginCheck(getAccount(), getPsw());
		if(adminstab != null){
			ctx.getSession().put("account",getAccount());
			adminstabDAO.getSession().getTransaction().commit();
			adminstabDAO.getSession().close();
			return SUCCESS;
		}
		else {
			adminstabDAO.getSession().getTransaction().commit();
			adminstabDAO.getSession().close();
			return ERROR;
		}
		
		
//		System.out.println(getAccount()+getPsw());
//		if(getAccount().equals("Marvin") && getPsw().equals("123")){
//			System.out.println("Yes");
//			return SUCCESS;
//		}
//		else {
//			System.out.println("No");
//			return ERROR;
//		}
	}

}
